package com.ruoyi.system.service.impl;

import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.constant.RedisConstant;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.baidu.GeneralBasic;
import com.ruoyi.common.utils.uuid.IdUtils;
import com.ruoyi.system.domain.*;
import com.ruoyi.system.mapper.TmcUserMapper;
import com.ruoyi.system.service.ITmcUserService;
import org.apache.commons.lang3.RandomStringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;

import java.lang.reflect.Array;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@Service
public class TmcUserServiceImpl implements ITmcUserService {
    @Autowired
    private TmcUserMapper tmcUserMapper;

    @Override
    public List<Map<String, Object>> queryFeeList(TmcUser tmcUser) {
        return tmcUserMapper.queryFeeList(tmcUser);
    }

    @Override
    public List<TmcHouseSubbill> queryFeeListById(TmcHouseSubbill tmcHouseSubbill) {
        return tmcUserMapper.queryFeeListById(tmcHouseSubbill);
    }

    @Override
    public List<TmcUser> queryList(TmcUser tmcUser) {
        return tmcUserMapper.queryList(tmcUser);
    }

    @Override
    public List<TmcUserHouse> queryHouseList(TmcUser tmcUser) {
        return tmcUserMapper.queryHouseList(tmcUser);
    }

    @Override
    public List<TmcUserStall> queryStallList(TmcUser tmcUser) {
        return tmcUserMapper.queryStallList(tmcUser);
    }

    @Override
    public List<TmcUserCarcode> queryCarcodeList(TmcUser tmcUser) {
        return tmcUserMapper.queryCarcodeList(tmcUser);
    }

    @Override
    public List<TmcEpidemicUser> queryEpidemicList(TmcEpidemicUser tmcEpidemicUser) {
        return tmcUserMapper.queryEpidemicList(tmcEpidemicUser);
    }

    @Override
    public List<TmcEpidemicUserRecord> queryEpidemicRecordList(TmcEpidemicUser tmcEpidemicUser) {
        return tmcUserMapper.queryEpidemicRecordList(tmcEpidemicUser);
    }

    public ClientUser selectClientUserById(Long id) {
        return tmcUserMapper.selectClientUserById(id);
    }

    @Autowired
    private StringRedisTemplate redisTemplate;

    @Override
    public UserInfoBean selectComUserAdmin(ComUserAdmin comUserAdmin) {
        List<ComUserAdmin> comUserAdmins = tmcUserMapper.selectComUserAdmin(comUserAdmin);
        UserInfoBean adminUser = new UserInfoBean();
        if (comUserAdmins.size() > 0) {
            String accessToken = IdUtils.fastUUID();
            adminUser.setAccessToken(accessToken);
            adminUser.setName(tmcUserMapper.queryClientName(comUserAdmins.get(0).getClientUserId()));
            adminUser.setServerCode("TMYJT");
            adminUser.setWeChatPublic_ID(1);
            adminUser.setStatus(1);
            adminUser.setID(comUserAdmins.get(0).getClientUserId());
            adminUser.setCompany_Name(tmcUserMapper.queryCompanyName(comUserAdmins.get(0).getClientUserId()));
            //存入redis
            redisTemplate.opsForValue().set(adminUser.getName(), adminUser.getAccessToken(), RedisConstant.EXPORE, TimeUnit.SECONDS);
            adminUser.setErrorMessage("登录成功");
        } else {
            adminUser.setErrorMessage("登录失败");
        }

        return adminUser;
    }

    /**
     * 查询【请填写功能名称】列表
     *
     * @param clientUser 【请填写功能名称】
     * @return 【请填写功能名称】
     */
    @Override
    public List<ClientUser> selectClientUserList(ClientUser clientUser) {
        return tmcUserMapper.selectClientUserList(clientUser);
    }

    /**
     * 新增【请填写功能名称】
     *
     * @param clientUser 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int insertClientUser(ClientUser clientUser) {
        if (tmcUserMapper.querySumBytel(clientUser.getUserTel()) == 0) {
            clientUser.setCreateTime(DateUtils.getNowDate());
            tmcUserMapper.insertClientUser(clientUser);
            return 1;
        } else {
            return 0;
        }
    }

    /**
     * 修改【请填写功能名称】
     *
     * @param clientUser 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int updateClientUser(ClientUser clientUser) {
        HashMap map = new HashMap();
        map.put("userTel", clientUser.getUserTel());
        map.put("id", clientUser.getId());
        if (tmcUserMapper.querySumByid(map) == 0) {
            return tmcUserMapper.updateClientUser(clientUser);
        } else {
            return 0;
        }
    }

    /**
     * 批量删除【请填写功能名称】
     *
     * @param ids 需要删除的【请填写功能名称】ID
     * @return 结果
     */
    @Override
    public int deleteClientUserByIds(Long[] ids) {
        return tmcUserMapper.deleteClientUserByIds(ids);
    }

    @Override
    public String SelectCity(String Cityname) {
        return tmcUserMapper.SelectCity(Cityname);
    }

    //行程信息上传
    @Override
    public int updateRecord(TmcEpidemicUserRecord tmcEpidemicUserRecord) {
        //判断识图结果是否为空
        try {
            String basic = GeneralBasic.generalBasic(RuoYiConfig.getProfile() + tmcEpidemicUserRecord.getAttachUrl().replaceAll("profile", ""));
            String red = "红色地区";
            String orange = "橙色地区";
            //更新判断红绿
            //去过的地方 比对疫情 给出是否去过红色橙色地区 1绿2橙3红
            if (basic.contains(red)) {
                tmcEpidemicUserRecord.setState(3L);
            }
            if (basic.contains(orange)) {
                tmcEpidemicUserRecord.setState(2L);
            }
            if (!basic.contains(orange) && !basic.contains(red)) {
                tmcEpidemicUserRecord.setState(1L);
            }
            tmcEpidemicUserRecord.setAttachUrlInfo(basic);
        } catch (Exception e) {
            tmcEpidemicUserRecord.setAttachUrlInfo("");
        }
        //结果不为空进入修改
        if (null!=tmcEpidemicUserRecord.getVisitred()){
            //判断文字选择 1绿2橙3红
            if (tmcEpidemicUserRecord.getVisitred() == 0 && tmcEpidemicUserRecord.getVisitorange() == 0) {
                tmcEpidemicUserRecord.setState(1L);
            }
            if (tmcEpidemicUserRecord.getVisitorange() == 1) {
                tmcEpidemicUserRecord.setState(2L);
            }
            if (tmcEpidemicUserRecord.getVisitred() == 1) {
                tmcEpidemicUserRecord.setState(3L);
            }
            //修改用户二维码状态
            tmcUserMapper.updateregistered(tmcEpidemicUserRecord);
        }
        //修改表
        return tmcUserMapper.updateRecord(tmcEpidemicUserRecord);
    }

    //新增行程信息
    @Override
    public Long insertRecord(TmcEpidemicUserRecord tmcEpidemicUserRecord) {
        //获取当前时间
        Calendar calendar = Calendar.getInstance();
        //生成一组随机字符串
        String code = RandomStringUtils.randomAlphanumeric(8) + "-" + RandomStringUtils.randomAlphanumeric(4) + "-" + RandomStringUtils.randomAlphanumeric(4) + "-" + RandomStringUtils.randomAlphanumeric(4) + "-" + RandomStringUtils.randomAlphanumeric(12);
        tmcEpidemicUserRecord.setQrcode(code);
        tmcEpidemicUserRecord.setCreatetime(calendar.getTime());
        //不为空则进入注册 添加用户的基本信息
        if (tmcEpidemicUserRecord.getIdnumber() != null) {
            tmcUserMapper.insertregistered(tmcEpidemicUserRecord);
        }
        //判断文字选择 1绿2橙3红
        if (tmcEpidemicUserRecord.getVisitred() == 0 && tmcEpidemicUserRecord.getVisitorange() == 0) {
            tmcEpidemicUserRecord.setState(1L);
        }
        if (tmcEpidemicUserRecord.getVisitorange() == 1) {
            tmcEpidemicUserRecord.setState(2L);
        }
        if (tmcEpidemicUserRecord.getVisitred() == 1) {
            tmcEpidemicUserRecord.setState(3L);
        }
        //修改用户二维码基础信息
        tmcUserMapper.updateregistered(tmcEpidemicUserRecord);
        //上报行程
        tmcUserMapper.insertRecord(tmcEpidemicUserRecord);
        //返回ID
        return tmcEpidemicUserRecord.getId();
    }

    @Override
    public TmcEpidemicUser queryUserInfo(Long id) {
        return tmcUserMapper.queryUserInfo(id);
    }

    @Override
    public List<TmcEpidemicUserRecord> queryUserRecord(Long publicuserId) {
        return tmcUserMapper.queryUserRecord(publicuserId);
    }

    @Override
    public int insertUser(TmcUser tmcUser) {
        return tmcUserMapper.insertUser(tmcUser);
    }

    @Override
    public TmcUser queryListById(Long id) {
        TmcUser tmcUser = tmcUserMapper.queryListById(id);
        List<TmcUserHouse> list = tmcUserMapper.queryHouseList(tmcUser);
        if(list.size()>0){
            tmcUser.setHouseList(list);
        }
        return tmcUser;
    }

    @Override
    public TmcEpidemicUserRecord queryUserRecordById(Long id) {
        return tmcUserMapper.queryUserRecordById(id);
    }


    /**
     * 删除【请填写功能名称】信息
     *
     * @param id 【请填写功能名称】ID
     * @return 结果
     */
    @Override
    public int deleteClientUserById(Long id) {
        return tmcUserMapper.deleteClientUserById(id);
    }

    public HashMap QueryUserParkingCarInfo(TmcUser tmcUser) {
        List<TmcUserStall> list = tmcUserMapper.queryStallList(tmcUser);
        List<TmcUserCarcode> tmcUserCarcodes = tmcUserMapper.queryCarcodeList(tmcUser);
        HashMap map = new HashMap();
        map.put("ParkingList", list);
        map.put("CarList", tmcUserCarcodes);
        return map;
    }

    @Override
    public int insertTmcUserCar(TmcUserCarcode tmcUserCar) {
        return tmcUserMapper.insertTmcUserCar(tmcUserCar);
    }

    @Override
    public int insertTmcUserFormid(TmcUserFormid tmcUserFormid) {
        return tmcUserMapper.insertTmcUserFormid(tmcUserFormid);
    }

    @Override
    public int updateTmcUserCar(TmcUserCarcode tmcUserCar) {
        return tmcUserMapper.updateTmcUserCar(tmcUserCar);
    }

    @Override
    public int deleteTmcUserCarById(Long id) {
        return tmcUserMapper.deleteTmcUserCarById(id);
    }

    @Override
    public int deleteTmcUserCarByIds(Long[] ids) {
        return tmcUserMapper.deleteTmcUserCarByIds(ids);
    }

    @Override
    public TmcUserCarcode selectTmcUserCarById(Long id) {
        return tmcUserMapper.selectTmcUserCarById(id);
    }

    @Override
    public TmcEpidemicUserRecord selectSubmit(TmcEpidemicUserRecord tmcEpidemicUserRecord) {
        //获取当前时间
        Calendar calendar = Calendar.getInstance();
        tmcEpidemicUserRecord.setCreatetime(calendar.getTime());
        return tmcUserMapper.selectSubmit(tmcEpidemicUserRecord);
    }

    @Override
    public int insertTmcUser(TmcUser user) {
        return tmcUserMapper.insertTmcUser(user);
    }

    @Override
    public int qureyopenIDsum(String openid) {
        return tmcUserMapper.qureyopenIDsum(openid);
    }
    @Override
    public int qureyPhonesum(String pPhone) {
        return tmcUserMapper.qureyPhonesum(pPhone);
    }

    @Override
    public TmcUser queryUser(TmcUser tmcUser) {
        return null;
    }

    @Override
    public int updateTmcUser(TmcUser tmcUser) {
        return tmcUserMapper.updateTmcUser(tmcUser);
    }

    @Override
    public List<District> selectDistrictList() {
        return tmcUserMapper.selectDistrictList();
    }
}
